--PRIMERA PREGUNTA ADVENTUREWORKS2012
--1.
/*
USE AdventureWorks2012
GO


DECLARE @ORIGINAL INT, @AUMENTO10 INT, @AUMENTO15 INT, @FECHA DATETIME
DECLARE QAUMENTO CURSOR fast_forward FOR
SELECT ProductID, Name, ProductNumber,ListPrice
		FROM Production.Product
OPEN QAUMENTO
--SE DEBE PONER LA PRIMERA POSICION ASI
FETCH NEXT FROM QAUMENTO INTO @ORIGINAL,@AUMENTO10,@AUMENTO15
WHILE @@FETCH_STATUS = 0
  BEGIN 
    SET @ORIGINAL =
	CASE 
	  WHEN @AUMENTO10 >=99 THEN (@AUMENTO10 * 0.10)
	  WHEN @AUMENTO15 < 100 (@AUMENTO15 * 0.15)
	  END 
	  FETCH NEXT FROM QAUMENTO INTO @ORIGINAL,@AUMENTO10,@AUMENTO15
END

CLOSE QPersona
DEALLOCATE QPersona


--SEGUNDA PREGUNTA 
--2.

USE AdventureWorks2012
GO
DECLARE  @FIBO TABLE (NUMEROFIBO INT)

DECLARE @PRIMERO INT
,@SEGUNDO INT 
,@FIBONACCI INT 

SET @PRIMERO = 1
SET @SEGUNDO = 0
SET @FIBONACCI = 0

WHILE @SEGUNDO <= 30000
BEGIN 
SET @FIBONACCI = @PRIMERO+@SEGUNDO
PRINT @FIBONACCI
SET @PRIMERO=@SEGUNDO
SET @SEGUNDO=@FIBONACCI
END

INSERT INTO @FIBO (NUMEROFIBO)
SELECT @SEGUNDO
SELECT * FROM @FIBO

 MERGE INTO NORTHWND.dbo.Customers AS CUS
	USING(
		SELECT * from Person.Person WHERE BusinessEntityID IN (SELECT * FROM @FIBO)
		) AS NEW ON NEW.BusinessEntityID = CUS.CustomerID

	WHEN MATCHED THEN UPDATE SET
	WHEN NOT MATCHED THEN INSERT()
		VALUES(src.FirstName + ISNULL(src.MiddleName,''), src.LastName, src.Title)
	OUTPUT HOST_NAME(), 'ANTES: ' + deleted.NOMBRE +''+ 'DESPUES: ' + inserted.NOMBRE
	  ,'update','Persona'
GO